package org.jeecg.modules.wangyue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.IsRoleAuthConstant;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.entity.QualificationReviewJs;
import org.jeecg.modules.wangyue.entity.ServiceJs;
import org.jeecg.modules.wangyue.entity.UserRoleInfo;
import org.jeecg.modules.wangyue.mapper.QualificationReviewJsMapper;
import org.jeecg.modules.wangyue.mapper.ServiceJsMapper;
import org.jeecg.modules.wangyue.mapper.UserRoleInfoMapper;
import org.jeecg.modules.wangyue.service.IQualificationReviewJsService;
import org.jeecg.modules.wangyue.service.IServiceJsService;
import org.jeecg.modules.wangyue.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 技师资质审核表
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Service
public class QualificationReviewJsServiceImpl extends ServiceImpl<QualificationReviewJsMapper, QualificationReviewJs> implements IQualificationReviewJsService {

    @Autowired
    private QualificationReviewJsMapper qualificationReviewJsMapper;
    @Autowired
    private UserRoleInfoMapper userRoleInfoMapper;
    @Autowired
    private ServiceJsMapper serviceJsMapper;
    @Autowired
    private IServiceJsService serviceJsService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean passReviewStatus(QualificationReviewJs qrj) {
        try{
            QualificationReviewJs qualificationReviewJs = qualificationReviewJsMapper.selectById(qrj.getId());
            //先把审核通过的申请数据写入该角色的技师信息里
            UserRoleInfo userRoleInfo = new UserRoleInfo();
            userRoleInfo.setHeadPicUrl(qualificationReviewJs.getRealHeadPicUrl()); //头像url
            userRoleInfo.setIsRoleAuth(IsRoleAuthConstant.yes); //是否角色认证(0-否,1-是)
            userRoleInfo.setSex(qualificationReviewJs.getSex()); //性别(0-未设置,1-男,2-女)
            userRoleInfo.setRealName(qualificationReviewJs.getRealName()); //真实姓名
            userRoleInfo.setIdCard(qualificationReviewJs.getIdCard()); //身份证
            userRoleInfo.setDateOfBirth(qualificationReviewJs.getDateOfBirth()); //出生年月
            userRoleInfo.setYear(qualificationReviewJs.getYear()); //出生年份
            userRoleInfo.setWorkingAge(qualificationReviewJs.getWorkHistory()); //工作年龄
            userRoleInfo.setPhone(qualificationReviewJs.getPhone()); //联系电话
            userRoleInfo.setLog(qualificationReviewJs.getLog()); //经度
            userRoleInfo.setLat(qualificationReviewJs.getLat()); //维度
            userRoleInfo.setAddressName(qualificationReviewJs.getAddressName()); //所在地址名
            userRoleInfo.setCity(qualificationReviewJs.getCity()); //所在城市
            userRoleInfo.setIdCardFrontUrl(qualificationReviewJs.getIdCardFrontUrl()); //身份证正面
            userRoleInfo.setIdCardBackUrl(qualificationReviewJs.getIdCardBackUrl()); //身份证反面
            userRoleInfo.setWorkProveUrl(qualificationReviewJs.getWorkProveUrl()); //职业证书
            userRoleInfo.setHealthProveUrl(qualificationReviewJs.getHealthProveUrl()); //健康证
            userRoleInfo.setUpdateTime(DateUtil.getTime("yyyy-MM-dd HH:mm:ss"));
            UpdateWrapper<UserRoleInfo> userRoleInfoUpdateWrapper = new UpdateWrapper<>();
            userRoleInfoUpdateWrapper.eq("user_id", qualificationReviewJs.getUserId());
            userRoleInfoUpdateWrapper.eq("user_type", RoleConstant.jishi);
            userRoleInfoUpdateWrapper.eq("del_flag", DelFlagConstant.normal);
            userRoleInfoMapper.update(userRoleInfo, userRoleInfoUpdateWrapper);

            //然后删除该技师已有的服务类型
            QueryWrapper<ServiceJs> serviceJsQueryWrapper = new QueryWrapper<>();
            serviceJsQueryWrapper.eq("user_id", qualificationReviewJs.getUserId());
            serviceJsQueryWrapper.eq("user_type", RoleConstant.jishi);
            serviceJsMapper.delete(serviceJsQueryWrapper);
            //再然后新增该技师送审的服务类型
            List<String> serviceTypeCodeList = Arrays.asList(qualificationReviewJs.getServiceTypeCode().split(","));
            List<ServiceJs> serviceJsList = new ArrayList<>();
            for(String serviceTypeCode : serviceTypeCodeList){
                ServiceJs serviceJs = new ServiceJs();
                serviceJs.setUserId(qualificationReviewJs.getUserId());
                serviceJs.setUserType(RoleConstant.jishi);
                serviceJs.setServiceTypeCode(serviceTypeCode);
                serviceJsList.add(serviceJs);
            }
            serviceJsService.saveBatch(serviceJsList);

            //最后状态改为：通过
            UpdateWrapper<QualificationReviewJs> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", qrj.getId());
            updateWrapper.eq("del_flag", DelFlagConstant.normal);
            updateWrapper.set("review_status", qrj.getReviewStatus());
            updateWrapper.set("update_time", DateUtil.getTime("yyyy-MM-dd HH:mm:ss"));
            qualificationReviewJsMapper.update(null, updateWrapper);

            return true;
        }catch (Exception e){
//            e.printStackTrace();
            throw new RuntimeException("技师资质审核-通过操作失败！");
        }
    }


}
